package com.lframework.xingyun.basedata.impl.make;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.utils.OpLogUtil;
import java.io.Serializable;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.web.core.utils.IdUtil;
import com.lframework.starter.common.utils.ObjectUtil;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.common.utils.Assert;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.xingyun.basedata.entity.MakeMethod;
import com.lframework.xingyun.basedata.mappers.MakeMethodMapper;
import com.lframework.xingyun.basedata.service.make.MakeMethodMaterialService;
import com.lframework.xingyun.basedata.service.make.MakeMethodService;
import com.lframework.xingyun.basedata.vo.make.CreateMakeMethodVo;
import com.lframework.xingyun.basedata.vo.make.QueryMakeMethodVo;
import com.lframework.xingyun.basedata.vo.make.UpdateMakeMethodVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MakeMethodServiceImpl extends BaseMpServiceImpl<MakeMethodMapper, MakeMethod> implements MakeMethodService {

    @Autowired
    private MakeMethodMaterialService makeMethodMaterialService;

    @Override
    public PageResult<MakeMethod> query(Integer pageIndex, Integer pageSize, QueryMakeMethodVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        IPage<MakeMethod> page = getBaseMapper().queryPage(vo);

        return PageResultUtil.convert(page);
    }

    @Override
    public List<MakeMethod> query(QueryMakeMethodVo vo) {

        return getBaseMapper().queryList(vo);
    }

    @Cacheable(value = MakeMethod.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public MakeMethod findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @OpLog(type = OtherOpLogType.class, name = "新增制作方法表，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String create(CreateMakeMethodVo vo) {

        MakeMethod data = new MakeMethod();
        data.setId(IdUtil.getId());
        data.setName(vo.getName());
        data.setDescription(vo.getDescription());
        data.setRelationMaterials(0);
        data.setRelationProducts(0);

        getBaseMapper().insert(data);

        makeMethodMaterialService.create(data, vo.getMaterials());

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);

        return data.getId();
    }

    @OpLog(type = OtherOpLogType.class, name = "修改制作方法表，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(UpdateMakeMethodVo vo) {

        MakeMethod data = getBaseMapper().selectById(vo.getId());
        if (ObjectUtil.isNull(data)) {
            throw new DefaultClientException("制作方法表不存在！");
        }

        LambdaUpdateWrapper<MakeMethod> updateWrapper = Wrappers.lambdaUpdate(MakeMethod.class)
                .set(MakeMethod::getName, vo.getName())
                .set(MakeMethod::getDescription, vo.getDescription())
                .set(MakeMethod::getAvailable, vo.getAvailable())
                .eq(MakeMethod::getId, vo.getId());

        getBaseMapper().update(updateWrapper);

        makeMethodMaterialService.create(data, vo.getMaterials());

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);
    }

    @CacheEvict(value = MakeMethod.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

